var admin = {};
var db = require('../../database');
var rs = require('../module/respond');

admin.getUsers = function (req,res) {
    var limit = req.query.limit || '20';
    var page = req.query.page || '1';
    var list = rs.copy(rs)
    db.query('select username , id , type , openid from user where status = 1 limit ' + (page-1)*limit + ' , ' + limit , function (err, rows) {
        if (err) {

        }
        else {
            list.success.content = rows;
        }
        res.send(rs.success)
    })
}

admin.disableUsers = function (req,res) {
    var id = req.body.id;
    var list = rs.copy(rs)
    new Promise(function(resolve, reject) {
        db.query('select * from user where id=?',id,function (err, rows) {
            if(err){
                console.log(err)
            }else{
                if(rows.length == 0){
                    return reject();
                }else{
                    return resolve(rows)
                }
            }

        })
    }).then(function () {
        db.query('update user set type="0" where id="'+id+'"',function (err,result) {
            if(err){

            }else{
                list.success.msg = '成功禁用该用户'
                // 使该用户的session失效
                // ？？
                // 不会
            }
            res.send(list.success)
        })
    }).catch(function () {
        list.fail.msg = '没有该用户';
        res.send(list.fail)
    })

}

module.exports = admin;